<template>
	<view>
		<view class="main">
			<view class="content" v-for="item in list">
				<questionnaire :title="item.title" :content="item.content" :time="item.createTime"
					:deadline="item.endTime" :teacher="item.publisherName"></questionnaire>
			</view>
			<u-loadmore :status="status" margin-top="15" margin-bottom="15" @loadmore="loadmore"/>
		</view>
	</view>
</template>

<script>
	import questionnaire from '../../components/questionnaire.vue'
	export default {
		data() {
			return {
				list:[],
				current: 1,
				size: 10,
				pages: 0,
				status: 'loading',
			}
		},
		components: {
			questionnaire
		},
		onLoad() {
			this.roleType = uni.getStorageSync("roleType")
			this.getAllVotingByStudent().then(() => {
				if(this.current >= this.pages) this.status = 'nomore';
				else this.status = 'loadmore';
			})
		},
		onReachBottom() {
			if (this.current >= this.pages) return;
			this.status = 'loading';
			this.current++;
			this.getAllVotingByStudent().then(() => {
				if (this.current >= this.pages) this.status = 'nomore';
				else this.status = 'loading';
			})

		},
		methods: {
			loadmore(){
				this.status = 'loading';
				this.current++;
				this.getAllVotingByStudent().then(() => {
					if(this.current >= this.pages) this.status = 'nomore';
					else this.status = 'loading';
				})
			},
			async getAllVotingByStudent() {
				const res = await this.$myRequest({
					url: "/voting/getAllVotingByStudent",
					data: {
						current: this.current,
						size: this.size
					}
				})
				this.list = [...this.list, ...res.data.data.records]

				// 获取页数
				this.pages = res.data.data.pages
				if (this.current >= this.pages) {
					this.status = 'nomore'
				}

				console.log(this.list);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 20upx;
	}
</style>
